package lc.字符串;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: qian
 * @createDate: 2021/10/24
 */
public class 单个字母组成的最长重复序列 {

    public String maxLen(String s) {

        s += " ";
        int i = 0;
        int j = 0;
        List<String> list = new ArrayList<>();
        char[] chars = s.toCharArray();
        // 判断里面没有重复的串
        int len = 0;
        for (int i1 = 0; i1 < chars.length - 1; i1++) {
            if (chars[i1] != chars[i1 + 1]) {
                len++;
            }
        }
        if (len == chars.length - 1) {
            System.out.println("无重复序列");
            return "";
        }
        // 找到 i - j 见的重复的串
        while (j < chars.length) {

            while (j < chars.length && chars[i] == chars[j]) {
                j++;
            }
            if (j < chars.length && chars[i] != chars[j]) {
                list.add(s.substring(i, j)); // 截取[i,j) 所有前面得多拼一个字符串不然越界了
            }
            i = j;
        }
        // 找到串中最大的
        String max ="";
        for (String s1 : list) {
            max = max.length() > s1.length() ? max : s1;
        }
        return max;
    }
}
